
package com.netease.xone.fragment;

import java.util.List;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.meta.Source;
import protocol.meta.Subject;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.Toast;

import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.adapter.SubjectAndSourceAdapter;
import com.netease.xone.widget.listview.LoadingExpandableListView;

public class FragmentSubjectAndSourceList extends FragmentBase {

    /**
     * 订阅，可展开主题列表（游戏、综合类）
     *
     * @param type
     * @see XoneConsts.SUBJECT_LIST_TYPE
     * @return
     */
    public static FragmentSubjectAndSourceList newInstance(String queryString) {
        FragmentSubjectAndSourceList fragment = new FragmentSubjectAndSourceList();
        Bundle b = new Bundle();
        b.putString(XoneConsts.BUNDLE_KEY.SEARCH_STRING, queryString);
        fragment.setArguments(b);
        return fragment;
    }
    private int mTid;
    private SubjectAndSourceAdapter mAdapter;
    private String mQueryString;
    private LoadingExpandableListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mQueryString = getArguments().getString(XoneConsts.BUNDLE_KEY.SEARCH_STRING);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_subject_and_source_list, null);
        list = (com.netease.xone.widget.listview.LoadingExpandableListView)v;
        list.getRefreshableView().setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if(mAdapter.getGroup(groupPosition) instanceof Source){
                    Source source = (Source)mAdapter.getGroup(groupPosition);
                    Toast.makeText(getActivity(), source.sourceName, Toast.LENGTH_SHORT).show();
                    return true;
                }else if(mAdapter.getGroup(groupPosition) instanceof Subject){
                    //Subject Item，默认操作，展开
                    return false;
                }else{
                    //Title Item，不响应
                    return true;
                }
            }
        });
        list.getRefreshableView().setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                    int childPosition, long id) {
                if(mAdapter.getChild(groupPosition, childPosition) != null){
                    Source source = mAdapter.getChild(groupPosition, childPosition);
                    Toast.makeText(getActivity(), source.sourceName, Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });
        mAdapter = new SubjectAndSourceAdapter(getActivity());
        XoneService.getInstance().addListener(mCallBack);
        mTid = XoneService.getInstance().doSearch(XoneConsts.SEARCH_TYPE.SUBJECT_AND_SOURCE, mQueryString);
        return v;
    }

    @Override
    public void onDestroyView() {
        XoneService.getInstance().removeListener(mCallBack);
        mAdapter.removeListener();
        super.onDestroyView();
    };

    XoneCallBack mCallBack = new XoneCallBack() {

        @Override
        public void onSearchSubjectAndSource(int transactionid, java.util.List<Object> subjectListAndSourceList) {
            if(mTid == transactionid){
                List<Subject> subjectList = (List<Subject>)subjectListAndSourceList.get(0);
                List<Source> sourceList = (List<Source>)subjectListAndSourceList.get(1);
                mAdapter.setData(mQueryString, subjectList, sourceList);
                list.getRefreshableView().setAdapter(mAdapter);
            }
        };

        @Override
        public void onSearchSubjectAndSourceError(int transactionId, int errCode, String err) {
            if(mTid == transactionId){
                Toast.makeText(getActivity(), err, Toast.LENGTH_SHORT).show();
            }
        };

    };
}
